﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mole
{
    public class FileExtensions
    {
        public List<FileConfig> FileCfg = new List<FileConfig>();

        public FileExtensions(String filepath)
        {
            try
            {
                if (System.IO.File.Exists(filepath))
                {
                    foreach (String line in System.IO.File.ReadAllLines(filepath))
                    {
                        if (!line.Contains('#') && (!String.IsNullOrEmpty(line)))
                        {
                            String[] splitcfg = line.Split(',');
                            FileCfg.Add(new FileConfig(splitcfg[0], splitcfg[1], splitcfg[2]));
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        public Boolean isMatch(String extension, String filetype, String filequery)
        {
            Boolean value = false;

            foreach (FileConfig cfg in FileCfg)
            {
                //look up if description matches what I've extracted
                if (filequery.Contains(cfg.Query))
                {
                    if (String.Equals(cfg.Type, filetype, StringComparison.OrdinalIgnoreCase))
                    {
                        value = true;
                        break;
                    }
                }
                else if (cfg.Query.Equals("*"))
                {
                    //look up by wildcard
                    if (String.Equals(extension.Trim('.'), cfg.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        value = true;
                        break;
                    }
                }
            }
            return value;
        }

        public Boolean ExtensionMisMatch(String extension, String filetype)
        {
            try
            {
                Boolean value = false;
                Boolean typefound = false;
                Boolean extfound = false;

                if (FindExactMatch(extension,filetype))
                {
                    return false;
                }


                if (FileTypeFound(filetype).Count() > 0)
                {
                    typefound = true;
                }

                if (ExtensionFound(extension).Count() > 0)
                {
                    extfound = true;
                }

                if (typefound && extfound)
                {
                    return true;
                }

                return value;
            }
            catch
            {
                throw;
            }
        }

        private Boolean FindExactMatch(String extension, String filetype)
        {
            foreach (FileConfig cfg in FileCfg)
            {
                if (String.Equals(cfg.Extension, extension, StringComparison.OrdinalIgnoreCase))
                {
                    if (filetype.Contains(cfg.Query))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        private FileConfig[] ExtensionFound(String extension)
        {

            List<FileConfig> filecfg = new List<FileConfig>();

            foreach (FileConfig cfg in FileCfg)
            {
                if (String.Equals(cfg.Extension, extension, StringComparison.OrdinalIgnoreCase))
                {
                    filecfg.Add(cfg);
                }
            }

            return filecfg.ToArray();
        }

        private FileConfig[] FileTypeFound(String filetype)
        {

            List<FileConfig> filecfg = new List<FileConfig>();

            foreach (FileConfig cfg in FileCfg)
            {
                if (filetype.Contains(cfg.Query))
                {
                    filecfg.Add(cfg);
                }
            }

            return filecfg.ToArray();
        }

    }
}
